package com.fat.spzx.manager.service.impl;

import cn.hutool.core.date.DateTime;
import com.fat.spzx.common.exception.AddSysRoleException;
import com.fat.spzx.manager.mapper.SysRoleMapper;
import com.fat.spzx.manager.mapper.SysRoleUserMapper;
import com.fat.spzx.manager.service.SysRoleService;
import com.fat.spzx.model.dto.system.SysRoleDto;
import com.fat.spzx.model.entity.system.SysRole;
import com.fat.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.sql.Date;
import java.text.DateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    /**
     * 分页查询角色列表
     */
    @Override
    public PageInfo<SysRole> findByPage(Integer current, Integer limit, SysRoleDto sysRoleDto) {
        //1.使用pageHelp开启分页
        PageHelper.startPage(current,limit);
        //2.根据条件查询
        List<SysRole> list = sysRoleMapper.findByPage(sysRoleDto);
        //3.封装在PageInfo中
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        //4.返回
        return pageInfo;
    }

    /**
     * 添加角色
     */
    @Override
    public void addSysRole(SysRole sysRole) {
        //1.判断参数是否为空
        if (ObjectUtils.isEmpty(sysRole)) throw new AddSysRoleException(ResultCodeEnum.DATA_ERROR);

        //2.判断名称是否重复
        SysRole sysRoleDB = sysRoleMapper.findByName(sysRole.getRoleName());
        if (!ObjectUtils.isEmpty(sysRoleDB)) throw new AddSysRoleException(ResultCodeEnum.SYSROLE_NAME_IS_EXISTS);

        //3.设置数据
        sysRole.setIsDeleted(0);

        //4.添加
        sysRoleMapper.addSysRole(sysRole);

    }

    /**
     * 修改角色
     */
    @Override
    public void updateSysRole(SysRole sysRole) {
        //1.判断参数是否为空
        if (ObjectUtils.isEmpty(sysRole)) throw new AddSysRoleException(ResultCodeEnum.DATA_ERROR);

        //2.判断是否存在id
        if (ObjectUtils.isEmpty(sysRole.getId())) throw new AddSysRoleException(ResultCodeEnum.DATA_ERROR);

        //3.修改更新时间
        sysRole.setUpdateTime(new Date(System.currentTimeMillis()));

        //4.向数据库发送请求修改
        sysRoleMapper.updateSysRole(sysRole);

    }

    /**
     * 删除角色
     */
    @Override
    public void deleteSysRole(Integer id) {
        //逻辑删除
        sysRoleMapper.deleteSysRole(id);
    }

    /**
     * 查询所有角色
     */
    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        //1.查询所有角色
        List<SysRole> list = sysRoleMapper.findAllRoles();

        //2.查询已选择的角色
        List<Long> roleIds =  sysRoleUserMapper.selectRoleIdByUserId(userId);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("allRolesList",list);
        resultMap.put("sysUserRoles",roleIds);

        return resultMap;
    }
}
